#include "ruleitemdelegate.h"
#include "global.h"
#include "ruleitemmodel.h"

#include <QComboBox>
#include <QLineEdit>
#include <QPrinterInfo>

RuleItemDelegate::RuleItemDelegate(QObject *parent) :
    QStyledItemDelegate(parent)
{
}

QWidget *RuleItemDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    QComboBox *combo = 0;
    switch (index.column()) {
    case RuleItemModel::AETitle:
        return new QLineEdit(parent);
        break;
    case RuleItemModel::Printer:
        combo = new QComboBox(parent);
        foreach (QPrinterInfo info, QPrinterInfo::availablePrinters()) {
            combo->addItem(info.printerName());
        }
        return combo;
    case RuleItemModel::EmptyImage:
    case RuleItemModel::Border:
        combo = new QComboBox(parent);
        combo->addItems(QStringList()<<tr("Client")<<tr("Black")<<tr("White"));
        return combo;
    case RuleItemModel::TrueSize:
        combo = new QComboBox(parent);
        combo->addItems(QStringList()<<tr("No")<<tr("Yes"));
        return combo;
    default:
        return QStyledItemDelegate::createEditor(parent, option, index);
    }
}

void RuleItemDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{
    PrintRule *rule = static_cast<PrintRule*>(index.internalPointer());
    switch (index.column()) {
    case RuleItemModel::AETitle:
        qobject_cast<QLineEdit*>(editor)->setText(rule->aetitle);
        break;
    case RuleItemModel::Printer:
        qobject_cast<QComboBox*>(editor)->setCurrentText(rule->printer);
        break;
    case RuleItemModel::EmptyImage:
        qobject_cast<QComboBox*>(editor)->setCurrentIndex(rule->emptyImage);
        break;
    case RuleItemModel::Border:
        qobject_cast<QComboBox*>(editor)->setCurrentIndex(rule->border);
        break;
    case RuleItemModel::TrueSize:
        qobject_cast<QComboBox*>(editor)->setCurrentIndex(rule->trueSize);
        break;
    }
}

void RuleItemDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
{
    switch (index.column()) {
    case RuleItemModel::AETitle:
        model->setData(index, qobject_cast<QLineEdit*>(editor)->text());
        break;
    case RuleItemModel::Printer:
        model->setData(index, qobject_cast<QComboBox*>(editor)->currentText());
        break;
    case RuleItemModel::EmptyImage:
    case RuleItemModel::Border:
    case RuleItemModel::TrueSize:
        model->setData(index, qobject_cast<QComboBox*>(editor)->currentIndex());
        break;
    }
}
